$(function () {
    var layer = layui.layer
    var form = layui.form
    initArtCateList()
    function initArtCateList() {
        $.ajax({
            method: 'GET',
            url: '/my/article/cates',
            success: function (res) {
                var htmlStr = template('tpl-table', res)
                $('#tplbox').html(htmlStr)
            }
        })
    }
    var addclose = null
    $('#btnAddCate').on('click', function () {
        addclose = layer.open({
            type: 1,
            title: '添加文章类别',
            area: ['500px', '250px'],
            content: $('#dialog-add').html()
        });
    })
    $('body').on('submit', '#form-add', function (e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize(),
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('新增文章列表失败！')
                }
                layer.msg('新增文章列表成功！')
                initArtCateList()
                layer.close(addclose)
            }
        })
    })
    var addedit = null
    $('tbody').on('click', '.btn-edit', function () {
        addedit = layer.open({
            type: 1,
            title: '编辑文章类别',
            area: ['500px', '250px'],
            content: $('#dialog-edit').html()
        });
        var id = $(this).attr('data-id')
        console.log(id);

        $.ajax({
            method: 'GET',
            url: '/my/article/cates/' + id,
            success: function (res) {
                form.val('form-edit', res.data)
            }
        })
    })
    $('body').on('submit', '#form-edit', function (e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize(),
            success: function (res) {
                console.log(res);
                if (res.status !== 0) {
                    return layer.msg('更新失败')
                }
                layer.msg('更新成功')
                layer.close(addedit)
                initArtCateList()
            }
        })
    })
    $('tbody').on('click', '.btn-delete', function () {
        var id = $(this).attr('data-Id')
        layer.confirm('确定要删除吗?', { icon: 3, title: '提示' }, function (index) {
            $.ajax({
                method: 'GET',
                url: '/my/article/deletecate/' + id,
                success: function (res) {
                    if (res.status !== 0) {
                        return layer.msg('删除文章失败！')
                    }
                    layer.msg('删除文章成功！')
                    layer.close(index);
                    initArtCateList()
                }
            })

        });
    })
})
